package com.woc.boot.admin.web;

import javax.validation.ConstraintViolationException;

import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.woc.boot.admin.entity.Response;

@RestControllerAdvice
public class GlobalException {

	  @ExceptionHandler(MethodArgumentNotValidException.class)
	  public Response<Object> handleError(MethodArgumentNotValidException e) {
		  Response<Object> resp = new Response<>();
		  BindingResult bindingResult = e.getBindingResult();
		  return resp.withError(bindingResult.getFieldError().getDefaultMessage());
	  }

	  @ExceptionHandler(ConstraintViolationException.class)
	  public Response<Object> handleError(ConstraintViolationException e) {
		  Response<Object> resp = new Response<>();
		  return resp.withError(e.getMessage());
	  }

	  @ExceptionHandler(BindException.class)
	  public Response<Object> handleError(BindException e) {
		  Response<Object> resp = new Response<>();
		  BindingResult bindingResult = e.getBindingResult();
		  return resp.withError(bindingResult.getFieldError().getDefaultMessage());
	  }
}
